package com.koo.common.base.controller;

import cn.hutool.core.date.DateUtil;
import com.koo.common.exception.RRException;
import com.koo.common.response.R;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PropFileConfig;
import com.koo.common.utils.file.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping(value = "/common")
public class FileUploadController {

    @Value("${file.host}")
    private String fileHost;

    //private static final String IMAGE_PATH = "http://file.asdzy.com/ImageHandler.ashx"; // 上传图 上传图片
    private static final String FILE_PATH = "http://file.asdzy.com/FileHandler.ashx"; // 上传文 上传文件

    /**
     * 文件上传
     */
    @RequestMapping("/upload")
    public R fileUpload(@RequestParam("file") CommonsMultipartFile file) {
        log.info("fileHost===", fileHost);
//        String IMAGE_PATH = PropFileConfig.getProperty("file.host");
        String filename = file.getOriginalFilename();
        String result = "";
        if (!filename.contains(".")) {
            throw new RRException("请上传带有后缀的文件");
        }
        String filestrExt = filename.substring(filename.lastIndexOf("."));
        filename = MyStrUtil.generate16ShortUUID() + filestrExt;
        try {
            File newFile = File.createTempFile("tmp", null);
            file.transferTo(newFile);
            newFile.deleteOnExit();
            String url = fileHost + "uploadFile?filename=" + filename;
            result = UploadUtil.postFile(newFile, url, filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String path = fileHost + "/upload/" + result;
        return R.ok().put(path);
    }

    /**
     * 文件上传
     */
    @PostMapping("/fileUpload2.action")
    public R fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) {
        String filename = file.getOriginalFilename();
        String filePath = "/" + PropFileConfig.getProperty("uploadPath") + "/" + DateUtil.format(new Date(), "yyyy/MM/dd") + "/" + MyStrUtil.generate16ShortUUID() + filename.substring(filename.lastIndexOf("."));
        String path = request.getServletContext().getRealPath("/") + filePath;
        File newFile = new File(path);
        try {
            if (!newFile.getParentFile().exists()) {
                newFile.getParentFile().mkdirs();
            }
            file.transferTo(newFile);
        } catch (IOException e) {
            throw new RRException("文件上传失败");
        }
        return R.ok().put(path);
    }


}